package com.yaoran.userauth.controller;

import com.yaoran.userauth.entity.Menu;
import com.yaoran.userauth.service.MenuService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {
    private final static Logger logger = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R menuAdd(@RequestBody Menu menu){
        menuService.addMenu(menu,null);
        return R.ok();
    }

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public R<PageUtils<Menu>> listMenu(@RequestBody Map<String,String> params){
        Map<String,Object> query = new HashMap<>();
        query.put("page",params.get("page"));
        query.put("limit",params.get("limit"));
        String keyWord = params.get("keyWord");
        int organizationId = Integer.valueOf(params.get("organizationId")).intValue();
        PageUtils<Menu> pageUtils = menuService.listMenu(new Query(query),keyWord,organizationId);
        return R.ok(pageUtils);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R updateMenu(@RequestBody Menu menu){
        menuService.updateMenu(menu);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R deleteMenu(@RequestBody Map<String,Long> params){
        menuService.deleteMenu(params.get("id"));
        return  R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/dels")
    public R deleteManyMenu(@RequestBody Map<String,Long[]> params){
        menuService.deleteManyMenu(params.get("ids"));
        return R.ok();
    }

    @ApiOperation("菜单详情")
    @GetMapping("/detail")
    public R<Menu> getMenu(@RequestParam Long id){
        Menu menu = menuService.getMenu(id);
        return R.ok(menu);
    }

    @ApiOperation("单个应用的菜单")
    @GetMapping("/appMenu")
    public R<List<Menu>> getApplicationMenu(@RequestParam Long applicationId){
        return R.ok();
    }
}
